public class Main {
    public static void main(String[] args) {
        Solution solution = new Solution();
        int[] fruits = new int[]{1,2,1};
        System.out.println(solution.totalFruit(fruits));
    }
}
class Solution {
    public int totalFruit(int[] fruits) {
        int n = fruits.length;
        int[] hash = new int[n];
        int mark = 0,ret = 0;
        for (int left=0,right=0;right<n;right++) {
            if (hash[fruits[right]]==0) mark++;//维护水果种类
            //进窗口
            hash[fruits[right]]++;
            //出窗口
            while (mark>2) {
                hash[fruits[left]]--;
                if (hash[fruits[left]]==0) mark--;//维护水果种类
                left++;
            }
            ret = Math.max(ret,right-left+1);
        }
        return ret;
    }
}